package com.ti.ouxun.util;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class ExitProgramUtil {
	
	private static final int time = 2000;
	private static boolean clickFirst = false;
	
	@SuppressLint("HandlerLeak")
	private static Handler myHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			clickFirst = false;
		}
	};
	
	public static void exit(Activity activity) {
		
		if( clickFirst ) {
			activity.finish();
		}else{
			startProcessTiming();
			Toast.makeText(activity, "再按一次退出", Toast.LENGTH_SHORT).show();
		}
		
	}
	
	// 开始线程计时
	private static void startProcessTiming() {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				clickFirst = true;
				try {
					Thread.sleep(time);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

				myHandler.sendEmptyMessage(0);
			}
		}).start();
	}

}
